package gov.ccswj.modules.apibusiness.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import gov.ccswj.modules.api.annotation.AuthIgnore;
import gov.ccswj.modules.apibusiness.service.ForeginManagementService;
import gov.ccswj.modules.apibusiness.service.PreciseInvestmentService;
import gov.ccswj.modules.apibusiness.utils.ApiR;

/**
 * @author KY.Cheng 大屏接口
 */
@RestController
@RequestMapping("/api/precise/investment")
public class PreciseInvestmentController {

	@Autowired
	private PreciseInvestmentService preciseInvestmentService;
	@Autowired
	private ForeginManagementService foreginManagementService;
	/**
	 * 首页
	 */
	@AuthIgnore
	@GetMapping("/map")
	public ApiR info(String area, Integer type, Integer industry, HttpServletResponse response) {

		if (area == null || type == null || industry == null) {
			return ApiR.ok().put("message","参数不能为空");
		}

		// 允许跨域
		response.setHeader("Access-Control-Allow-Origin", "*");
		List<Map<String, Object>> list = preciseInvestmentService.query(area, type, industry);

		if (list.size() == 0) {
			return ApiR.ok().put("data",new ArrayList<>());
		}

		Map<String, Object> mapname = new HashMap<String, Object>();

		mapname.put("name", list.get(0).get("value"));
		mapname.put("value", list.size());

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("count", mapname);
		map.put("company", list);
		System.out.println(list.size());

		return ApiR.ok().put("data", map);
	}

	/**
	 * 政策
	 * 
	 * @param response
	 * @return
	 */
	@AuthIgnore
	@GetMapping("/policy")
	public ApiR policy(HttpServletResponse response) {

		// 允许跨域
		response.setHeader("Access-Control-Allow-Origin", "*");
		List<Map<String, Object>> list = preciseInvestmentService.policy();

		if (list.size() == 0) {
			return ApiR.ok().put("data",new ArrayList<>());
		}

		return ApiR.ok().put("data", list).put("n", list.size());
	}

	/**
	 * 详情页
	 * 
	 * @param id
	 * @param response
	 * @return
	 */
	@AuthIgnore
	@GetMapping("/policyid")
	public ApiR policyid(Integer id, HttpServletResponse response) {

		// 允许跨域
		response.setHeader("Access-Control-Allow-Origin", "*");

		if (id == null) {
			return ApiR.ok().put("message","参数不能为空");
		}
		List<Map<String, Object>> list = preciseInvestmentService.policyid(id);

		if (list.size() == 0) {
			return ApiR.ok().put("data",new ArrayList<>());
		}

		return ApiR.ok().put("data", list.get(0));
	}

	/**
	 * 产业园区
	 * 
	 * @param response
	 * @return
	 */
	@AuthIgnore
	@GetMapping("/industrypark")
	public ApiR industrypark(String s, Integer n, HttpServletResponse response) {
		// 允许跨域
		response.setHeader("Access-Control-Allow-Origin", "*");

		if (s == null || n == null) {
			return ApiR.ok().put("message","参数不能为空");
		}

		List<Map<String, Object>> list = preciseInvestmentService.industrypark(s, n);

		int total = preciseInvestmentService.count("b_n_indusparks");

		if (list.size() == 0) {
			return ApiR.ok().put("data",new ArrayList<>());
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("data", list);
		map.put("total", total);

		return ApiR.ok().put("data", list).put("total", total);
	}

	/**
	 * 明星企业
	 * 
	 * @param response
	 * @return
	 */
	@AuthIgnore
	@GetMapping("/famouscompany")
	public ApiR famouscompany(String s, Integer n, HttpServletResponse response) {
		// 允许跨域
		response.setHeader("Access-Control-Allow-Origin", "*");

		if (s == null || n == null) {
			return ApiR.ok().put("message","参数不能为空");
		}

		List<Map<String, Object>> list = preciseInvestmentService.famouscompany(s, n);
		int total = preciseInvestmentService.count("b_n_famouscompany");

		if (list.size() == 0) {
			return ApiR.ok().put("data",new ArrayList<>());
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("data", list);
		map.put("total", total);

		return ApiR.ok().put("data", map);

	}

	/**
	 * 产业关系图
	 * 
	 * @param response
	 * @return
	 */
	@AuthIgnore
	@GetMapping("/industry")
	public ApiR industry(HttpServletResponse response) {
		// 允许跨域
		response.setHeader("Access-Control-Allow-Origin", "*");

		List<Map<String, Object>> list = preciseInvestmentService.industry();

		if (list.size() == 0) {
			return ApiR.ok().put("data",new ArrayList<>());
		}
		Map<String, Object> map = new HashMap<String, Object>();

		for (int i = 0; i < list.size(); i++) {
			Map<String, Object> map1 = new HashMap<String, Object>();
			map1.put("name", list.get(i).get("title").toString());
			map1.put("child", list.get(i).get("child_id").toString());
			map.put(switchWord(i), map1);
		}
		
		Map<String, Object> link1 = new HashMap<String, Object>();
		link1.put("source", "能源电力");
		link1.put("target", "新能源");
		Map<String, Object> link2 = new HashMap<String, Object>();
		link2.put("source", "能源电力");
		link2.put("target", "石油");
		Map<String, Object> link3 = new HashMap<String, Object>();
		link3.put("source", "能源电力");
		link3.put("target", "太阳能");
		Map<String, Object> link4 = new HashMap<String, Object>();
		link4.put("source", "能源电力");
		link4.put("target", "煤炭");

		List<Map<String, Object>> links = new ArrayList<Map<String,Object>>() ;
		links.add(link1);
		links.add(link2);
		links.add(link3);
		links.add(link4);
		
		return ApiR.ok().put("data", map).put("links", links);

	}

	public String switchWord(int i){
		switch(i){
		case(0): return "one";
		case(1): return "two";
		case(2): return "three";

		default : return "";
		}
	}

	/**
	 * 重点产业
	 * 
	 * @param response
	 * @return
	 */
	@AuthIgnore
	@GetMapping("/keyindustry")
	public ApiR keyindustry(String s, Integer n, Integer type, HttpServletResponse response) {
		// 允许跨域
		response.setHeader("Access-Control-Allow-Origin", "*");
		if (s == null || type == null || n == null) {
			return ApiR.ok().put("message","参数不能为空");
		}
		List<Map<String, Object>> list = preciseInvestmentService.keyindustry(s, n, type);
		int total = preciseInvestmentService.count("b_n_industry");
		if (list.size() == 0) {
			return ApiR.ok().put("data",new ArrayList<>());
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("data", list);
		map.put("total", total);

		return ApiR.ok().put("data", map);

	}
	
	/**
	 * 趋势分析      电子上午
	 * 
	 * @param response
	 * @return
	 */
	@AuthIgnore
	@GetMapping("/trendanalysis")
	public ApiR qushifenxi(String time, Integer msgtype, HttpServletResponse response) {
		// 允许跨域
		response.setHeader("Access-Control-Allow-Origin", "*");

		if(time.equals("2")|| time == null || time.equals("")){
			
		}else{
			return ApiR.ok().put("data",new ArrayList<>());
		}
		
		List<Map<String, Object>> list2017 = foreginManagementService.inoutyear("2017");
		
		List<String> xlist=new ArrayList<>();
		List<String> list17=new ArrayList<>();
		List<String> list16=new ArrayList<>();
		for(Map<String, Object> map: list2017){
			xlist.add(map.get("month").toString());
		}
		
		for(Map<String, Object> map: list2017){
			list17.add(map.get("data").toString());
			list16.add(map.get("month").toString());
		}

		Map<Object, Object> histogram = new HashMap<Object, Object>();
		List<String> strs=new ArrayList<>();
		strs.add("机械产品");
		strs.add("工业制成品");
		strs.add("半加工成品");
		histogram.put("datax", strs);
		histogram.put("datay", list17);
		
		
		List<Map<String, Object>> pie = foreginManagementService.queryInandout("5");
		
		

		Map<Object, Object> map = new HashMap<Object, Object>();
		Map<Object, Object> map1 = new HashMap<Object, Object>();
		
		map1.put("name", "本地数据");
		map1.put("data", list17);
		map.put("datax", list16);
		map.put("datay", map1);
		
		
		
		Map<String, Object> details = new HashMap<String, Object>();
		details.put("positive", "");
		details.put("negative", "");
		details.put("neutral", "");
		details.put("number", "");
		details.put("time", "");
		
		
		Map<String, Object> side = new HashMap<String, Object>();
		side.put("datay", "[]");
		side.put("datax", "[]");
		

		return ApiR.ok().put("pie", pie).put("line", map).put("details", details).put("side", side).put("histogram", histogram);

	}
	
	/**
	 *统计报表 4.1   电子上午
	 */
	@AuthIgnore
	@GetMapping("/statistical")
	public ApiR industryarticle(Integer s ,Integer n,HttpServletResponse response) {
		
		// 允许跨域
		response.setHeader("Access-Control-Allow-Origin", "*");
		if(s==null || n==null){
			return ApiR.ok().put("message","参数不能为空");
		}
		
		List<Map<String, Object>> list = foreginManagementService.industryarticle(s,n);

		int total =foreginManagementService.count("b_l_industry_article");
		
		if(list.size()==0 ){
			return ApiR.ok().put("data",new ArrayList<>());
		}
		
		
		Map<String, Object> map = new HashMap<>();
		
		return ApiR.ok().put("total", total).put("data", list);
	}
	
	/**
	 *详情
	 */
	@AuthIgnore
	@GetMapping("/statisticalid")
	public ApiR industryarticleid(Integer id,HttpServletResponse response) {
		

		// 允许跨域
		response.setHeader("Access-Control-Allow-Origin", "*");
		if(id==null ){
			return ApiR.ok().put("message","参数不能为空");
		}
		
		List<Map<String, Object>> list = foreginManagementService.industryarticleid(id);

		
		if(list.size()==0 ){
			return ApiR.ok().put("data",new ArrayList<>());
		}
		
		
		Map<String, Object> map = new HashMap<>();
		
		return ApiR.ok().put("data", list);
	}
	
	
	
	
	
}
